﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
#if !SILVERLIGHT
using System.IO.Packaging;
#endif
using System.Windows.Navigation;
using UIT.WPF.Renderers;

#if !SILVERLIGHT
namespace UIT.WPF
#else
namespace UIT.Silverlight
#endif
{
    public class Context : IUitContext
    {
        public event Action<List<string>> RolesNeeded;

        private class SecurityContext : ISecurityContext
        {
            private Context context;
            public SecurityContext(Context context)
            {
                this.context = context;
            }

            #region ISecurityContext Members

            public System.Security.Principal.IPrincipal User
            {
#if !SILVERLIGHT
                get { return Thread.CurrentPrincipal; }
#else
                get { throw new NotImplementedException(); }
#endif
            }

            List<string> roles;

            public string[] Roles
            {
                get
                {
                    if (roles == null && context != null && context.RolesNeeded != null)
                    {
                        roles = new List<string>();
                        context.RolesNeeded(roles);
                    }
                    return roles.ToArray();
                }
            }

            #endregion
        }

        #region IUitContext Members

        public string Mode
        {
            get { return "WPF"; }
        }

        public ISecurityContext Security
        {
            get { return new SecurityContext(this); }
        }

        public bool IsAllowed(string resource, string verb)
        {
            return NAcl.AclManager.IsAllowed(resource, verb, Security.Roles);
        }

        #endregion

        #region IUitContext Members


        public System.IO.Stream GetStream(Uri uri)
        {
            return System.Windows.Application.GetResourceStream(uri).Stream;
        }

        #endregion

        #region IUitContext Members

        public UIT.Application Application
        {
#if !SILVERLIGHT
            get { return ((UIT.WPF.Application)UIT.WPF.Application.Current).application; }
#else
            get { return ((UIT.Silverlight.Application)UIT.Silverlight.Application.Current).application; }
#endif
        }

        public Uri ResolveUrl(Uri uri)
        {
            if (uri.IsAbsoluteUri)
                return uri;

#if !SILVERLIGHT
            if (!uri.OriginalString.StartsWith("/"))
                uri = new Uri('/' + uri.OriginalString, UriKind.Relative);
            return PackUriHelper.Create(new Uri("application:///"), uri);
#else
            return uri;
#endif
        }

        #endregion

        public Animations.IStoryBoardManager Storyboard
        {
            get { throw new NotImplementedException(); }
        }

        public VisualStates.IVisualStateManager VisualState
        {
            get { throw new NotImplementedException(); }
        }

        #region IUitContext Members

        Dictionary<string, object> items = new Dictionary<string, object>();

        public object this[string key]
        {
            get
            {
                object result;
                items.TryGetValue(key, out result);
                return result;
            }
            set
            {
                items[key] = value;
            }
        }

        #endregion

        #region IUitContext Members


        public RendererManager RendererManager
        {
            get
            {
#if !SILVERLIGHT
                return new WpfRendererManager((Application)WPF.Application.Current).Manager;
#else
            return new SlRendererManager((Application)Sl.Application.Current).Manager;
#endif
            }
        }

        #endregion
    }
}
